package bg.btanev.eightyfour.gwtfacade.commandhandler;

import org.apache.commons.lang.Validate;

import bg.btanev.eightyfour.domain.Target;
import bg.btanev.eightyfour.domain.person.Address;
import bg.btanev.eightyfour.domain.person.ContactInfo;
import bg.btanev.eightyfour.domain.person.Person;
import bg.btanev.eightyfour.domain.person.PersonName;
import bg.btanev.eightyfour.gwtfacade.converter.DtoConverter;
import bg.btanev.eightyfour.service.TargetService;
import bg.btanev.eightyfour.shared.command.CommandResult;
import bg.btanev.eightyfour.shared.command.target.CreateTargetCommand;
import bg.btanev.eightyfour.shared.command.target.CreateTargetInput;
import bg.btanev.eightyfour.shared.dto.TargetDto;
import bg.btanev.eightyfour.shared.dto.person.AddressDto;
import bg.btanev.eightyfour.shared.dto.person.ContactInfoDto;
import bg.btanev.eightyfour.shared.dto.person.PersonDto;
import bg.btanev.eightyfour.shared.dto.person.PersonNameDto;

/**
 *
 * @author B.Tanev
 *
 */
public class CreateTargetCommandHandler
		extends
		GenericCommandHandler<CreateTargetInput, TargetDto, CommandResult<CreateTargetInput, TargetDto>, CreateTargetCommand> {

	private final TargetService targetService;
	private final DtoConverter converter;

	public CreateTargetCommandHandler(TargetService targetService, DtoConverter converter) {
		super();

		Validate.notNull(targetService);
		Validate.notNull(converter);

		this.targetService = targetService;
		this.converter = converter;
	}

	@Override
	protected TargetDto doExecuteInternal(CreateTargetInput attachedInput) {
		Validate.notNull(attachedInput);

		PersonDto personInput = attachedInput.getPerson();
		Validate.notNull(personInput);

		Validate.isTrue(personInput.getId() == null || personInput.getId().longValue() == 0);

		PersonNameDto personNameInput = personInput.getPersonName();
		ContactInfoDto contactInfoInput = personInput.getContactInfo();
		AddressDto addressInput = contactInfoInput.getAddress();

		PersonName personName = new PersonName(personNameInput.getFirstName(), personNameInput.getSurName(),
				personNameInput.getLastName());

		Address address = new Address(addressInput.getCountry(), addressInput.getCity(), addressInput.getStreet());
		ContactInfo contactInfo = new ContactInfo(contactInfoInput.getEmail(), contactInfoInput.getTelephone(),
				address, contactInfoInput.getWebSite());

		Person person = new Person(personName, contactInfo);
		Target target = targetService.createAndStoreTarget(person);

		return converter.domainToDto(target, 2);
	}

	@Override
	protected CommandResult<CreateTargetInput, TargetDto> packCommandResult(CreateTargetInput input,
			TargetDto detachedResult) {

		return new CommandResult<CreateTargetInput, TargetDto>(input, detachedResult);
	}

	@Override
	public CreateTargetInput attach(CreateTargetInput input) {
		return input;
	}

	@Override
	public TargetDto detach(TargetDto result) {
		return result;
	}
}
